<?php

namespace App\Http\Controllers\Api;

use App\Http\Resources\ArticleResource;
use App\Http\Resources\TopicResource;
use App\Services\MemberService;
use Illuminate\Http\Request;

class MemberInfoController extends Controller
{
    public function info(Request $request, MemberService $service)
    {
        $memberId = (int)$request->input('member_id');
        if (!$memberId) return $this->error('会员ID不能为空');

        if (auth('api')->check()) {
            $currentMemberId = auth('api')->user()->id;
        }

        return $this->_success($service->info($memberId, $currentMemberId ?? 0));
    }

    public function article(Request $request, MemberService $service)
    {
        $publisherId = $request->input('publisher_id');

        if (!$publisherId) return $this->error('发布者ID不能为空');

        return $this->_success(ArticleResource::collection($service->article($publisherId)));
    }

    public function topic(Request $request, MemberService $service)
    {
        $memberId = $request->input('member_id');
        if (!$memberId) return $this->error('会员ID不能为空');

        if (auth('api')->check()) {
            $currentMemberId = auth('api')->user()->id;
        }

        return $this->_success(TopicResource::collection($service->topic($memberId, $currentMemberId ?? 0)));
    }

    public function collect(Request $request, MemberService $service)
    {
        $memberId = $request->input('member_id');
        if (!$memberId) return $this->error('会员ID不能为空');

        return $this->_success($service->collect($memberId));
    }

    public function comment(Request $request, MemberService $service)
    {
        $memberId = $request->input('member_id');
        if (!$memberId) return $this->error('会员ID不能为空');

        if (auth('api')->check()) {
            $currentMemberId = auth('api')->user()->id;
        }

        return $this->_success($service->comment($memberId, $currentMemberId ?? 0));
    }
}
